//
//  ViewController.swift
//  MyChineseChess
//
//  Created by bao on 2021/7/1.
//

import UIKit
import SnapKit

class ViewController: UIViewController {
    
    let myPlayButton = UIButton()

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.
        
        initView()
    }

    func initView(){
        let myImageView = UIImageView(image: UIImage(named: "bgImage"))
        self.view.addSubview(myImageView)
        
        myImageView.snp.makeConstraints { make in
            make.left.equalTo(self.view.snp.left)
            make.right.equalTo(self.view.snp.right)
            make.top.equalTo(self.view.snp.top)
            make.bottom.equalTo(self.view.snp.bottom)
        }
        
        
        let myStartButton = UIButton()
        myStartButton.setTitle("开始游戏", for: UIControl.State.normal)
        myStartButton.layer.borderWidth = 2
        myStartButton.layer.borderColor = UIColor.orange.cgColor
        myStartButton.addTarget(self, action: #selector(btnStartHandler), for: UIControl.Event.touchUpInside)
        self.view.addSubview(myStartButton)
        myStartButton.snp.makeConstraints { make in
            make.centerX.equalTo(self.view.snp.centerX)
            make.centerY.equalTo(self.view.snp.centerY)
            make.width.equalTo(100)
            make.height.equalTo(30)
        }
        
        let myLabel = UILabel()
        myLabel.text = "中国象棋"
        myLabel.font = UIFont.boldSystemFont(ofSize: 28)
        myLabel.textColor = UIColor.red
        self.view.addSubview(myLabel)
        myLabel.snp.makeConstraints { make in
            make.centerX.equalTo(self.view.snp.centerX)
            make.centerY.equalTo(self.view.snp.centerY).offset(-200)
            make.width.equalTo(150)
            make.height.equalTo(60)
        }
        
        
        
        var playTitle:String = "播放音乐（关）"
        if MyMusicPlayer.sharedInstance.getPlayStatus() {
            playTitle = "播放音乐（开）"
            MyMusicPlayer.sharedInstance.playMusic()
        } else {
            MyMusicPlayer.sharedInstance.stopMusic()
        }
        
        myPlayButton.setTitle(playTitle, for: UIControl.State.normal)
        myPlayButton.layer.borderWidth = 2
        myPlayButton.layer.borderColor = UIColor.orange.cgColor
        myPlayButton.addTarget(self, action: #selector(btnPlayHandler), for: UIControl.Event.touchUpInside)
        myPlayButton.titleLabel?.adjustsFontSizeToFitWidth = true
        self.view.addSubview(myPlayButton)
        myPlayButton.snp.makeConstraints { make in
            make.centerX.equalTo(self.view.snp.centerX)
            make.centerY.equalTo(self.view.snp.centerY).offset(100)
            make.width.equalTo(150)
            make.height.equalTo(30)
        }
        
    }
    
    @objc func btnPlayHandler() {
        var playTitle:String = "播放音乐（开）"
        if MyMusicPlayer.sharedInstance.getPlayStatus() {
            playTitle = "播放音乐（关）"
            MyMusicPlayer.sharedInstance.stopMusic()
            
        } else {
            MyMusicPlayer.sharedInstance.playMusic()
        }
        
        myPlayButton.setTitle(playTitle, for: UIControl.State.normal)
    }
    
    @objc func btnStartHandler() {
        let vc = MyGameViewController()
        vc.modalPresentationStyle = UIModalPresentationStyle.fullScreen
        self.present(vc, animated: true, completion: nil)
    }
    
}

